package com.baoyang.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baoyang.tool.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // 如果有 createBy、updateBy 也可以在这里填充
        // 获取当前用户ID（你需要实现获取当前登录用户ID的方法）
        String userId = SecurityUtils.getCurrentUserId();
        this.strictInsertFill(metaObject, "createBy", String.class, userId);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        String userId = SecurityUtils.getCurrentUserId();
        this.strictUpdateFill(metaObject, "updateBy", String.class, userId);
    }
}